package com.itheima.service.Impl;

import com.itheima.mapper.OrdersMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderItems;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Service
public  class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 添加 订单和订单详情表
     * @param order
     */
    @Transactional//事务
    @Override
    public void add(Order order) {
        ordersMapper.add(order);//用户添加订单
        Integer id = order.getId();//获取订单的Id
        List<OrderItems> items = order.getItems(); //获取订单的菜单列表
        if (!CollectionUtils.isEmpty(items)){
            ordersMapper.addEI(id,items);//添加订单详情(订单表和菜单表关联)1对多
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        ordersMapper.delete(id);
    }


    @Override
    public List<Order> listClient(Integer gid) {
     List<Order> list = ordersMapper.listClient(gid);
     return list;
    }

    @Override
    public List<Order> selectSOrder(Integer sId) {
        List<Order> list = ordersMapper.selectSOrder(sId);
        return list;
    }
}
